<template>
    <div id="main">
        <div class="bar">
            <div class="left">
                菜单列表
            </div>
            <div class="right">
                <el-button type="success" size="mini"  @click="addMenu"> <i class="el-icon-plus"></i> 添加菜单</el-button>
            </div>
        </div>
        <div class="table">
            <el-table
                :data="tableData"
                style="width: 100%;margin-bottom: 20px;"
                row-key="id"
                border
                default-expand-all
                :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
                <el-table-column
                prop="id"
                label="菜单ID"
                align="center"
                sortable
               >
                </el-table-column>
                <el-table-column
                prop="title"
                align="center"
                label="菜单名称"
                sortable
                >
                </el-table-column>
                <el-table-column
                prop="name"
                align="center"
                label="路由">
                </el-table-column>
                <el-table-column
                prop="pic"
                align="center"
                label="图标">
                    <template slot-scope="scope">
                        <img class="pic" :src="scope.row.pic" alt="" >
                    </template>
                </el-table-column>
                <el-table-column
                prop="sort"
                align="center"
                label="排序">
                </el-table-column>
                <el-table-column
                prop="status"
                align="center"
                label="状态">
                    <template slot-scope="scope">
                        <el-button :type="scope.row.status == 1 ? 'success': 'danger' " size="mini" @click="changeStatus(scope.row.id)">{{ scope.row.status == 1 ? "开启" : "关闭" }}</el-button>
                    </template>
                </el-table-column>

                <el-table-column
               
                align="center"
                label="操作">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="editMenu(scope.row)">编辑</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>

        <!-- dialog -->
        <menu-dialog :DialogData="DialogData" :FormData="FormData" @update="getAllMenu"></menu-dialog>
    </div>
</template>

<script>
    import MenuDialog from "@/components/dialog/MenuDialog"
    export default {
        data() {
            return {
                tableData: [],
                DialogData: {
                    title: "",
                    show: false,
                },
                FormData: {},
            }
        },
        components: {
            MenuDialog,
        },
        computed: {
            token() {
                return localStorage.token; 
            }
        },
        mounted() {
            this.getAllMenu()
        },
        methods: {
            // 获取所有菜单
            async getAllMenu() {
                let res =await this.$axios.post("/admin/role/menu_module_show", { token: this.token })
                // console.log(res);
                if(res.data.code == 200) {
                    this.tableData = res.data.data
                }
                
            },
            // 编辑菜单
            editMenu( user ) {
                this.DialogData = {
                    show: true,
                    title: "编辑菜单",
                    add: false,
                    edit: true
                }
                this.FormData = user
            },
            // 添加菜单
            addMenu() {
                this.DialogData = {
                    show: true,
                    title: "添加菜单",
                    add: true,
                    edit: false
                }
                this.FormData = {}
            },
            // 菜单状态改变
            async changeStatus( id ) {
                let res = await this.$axios.post("/admin/role/menu_module_change", {token: this.token, id })
                // console.log(res);
                if(res.data.code == 204) {
                    this.$message.success("更新成功")
                    this.getAllMenu()
                } else {
                    this.$message.success("更新失败")
                    this.getAllMenu()
                }
                
            },
        },
    }
</script>

<style lang="scss" scoped>
 #main {
     width: 100%;
     box-sizing: border-box;
     .bar {
         width: 100%;
         font-weight: bold;
         display: flex;
         justify-content: space-between;
         height: 40px;
         line-height: 40px;
         border-bottom: 1px solid #aaa;
     }
     .table {
         margin-top: 20px;
     }
     .pic {
         width: 40px;
         height: 40px;
         display: block;
     }
 }
</style>